/*
 *
 */

jsx3.Class.defineClass(
        "tibco.admin.enterpriseAssets.shdResDefs.EMS.ShdEmsResDefs", //the full name of the class to create
        jsx3.lang.Object, //  extends class
        [tibco.admin.enterpriseAssets.shdResDefs.ShdResDefsInterface], // implements interface
        function (plugin) {

            plugin.prototype.details = null;
            // TODO: handle multiple instances?

            // Implementation of the ShdResDefsInterface
            plugin.instance = null

            plugin.prototype.initPlugin = function() {
                tibco.admin.Main.loadResource("tibco/admin/enterpriseAssets/shdResDefs/EMS/jss/jss.xml", "tibco.admin", "jss_25F", "ljss", false);
                tibco.admin.Main.loadResource("tibco/admin/enterpriseAssets/shdResDefs/EMS/jss/valueJSS.xml", "tibco.admin", "value_25F", "jss", false);
            }

            plugin.prototype.destroyPlugin = function() {
            }

            plugin.getInstance = function(args) {
                if(!this.instance) {
                    this.instance = new tibco.admin.enterpriseAssets.shdResDefs.EMS.ShdEmsResDefs();
                }
                return this.instance;
            }

            plugin.prototype.loadComponent = function(parent, master, id, name, description) {
                var me = this;
                this.details = new tibco.admin.enterpriseAssets.shdResDefs.EMS.ShdEmsResDefDetails(master, parent, id,
                        name, description, me);
                this.details.loadView();
                return this.details;
            }

            plugin.prototype.getType = function() {
                return "JMS";
            }

            plugin.prototype.getAddText = function() {
                return tibco.admin.Main.getServer().getDynamicProperty("@AddText_EMS");
            }

            plugin.prototype.getImagePath = function() {
                return tibco.admin.Main.getServer().getDynamicProperty("@addin@EMS Icon Path");
            }

            plugin.prototype.addSharedResourceDefinition = function(parent, master) {
                var me = this;
                this.details = new tibco.admin.enterpriseAssets.shdResDefs.EMS.ShdEmsResDefDetails(master, parent, null,
                        null, null, me);
                this.details.loadAdd();
                return this.details;
            }
            // End implementation of the ShdResDefsInterface

        });

jsx3.Class.defineClass(
        "tibco.admin.enterpriseAssets.shdResDefs.EMS.ShdEmsResDefDetails", //the full name of the class
        tibco.admin.enterpriseAssets.shdResDefs.ShdResDefDetails, //  extends ShdResDefDetails
        null, // implements no interface
        function (details) {
            details.ONTYPESELECTED = "tibco.admin.enterpriseAssets.shdResDefs.EMS.TYPESELECTED";
            details.ONPICKJNDI = "tibco.admin.enterpriseAssets.shdResDefs.EMS.PICKJNDI";
            details.ONTESTCONNECTION = "tibco.admin.enterpriseAssets.shdResDefs.EMS.TESTCONNECTION";

            details.prototype.subType = null;
            details.prototype.helpID = "EnterpriseAssets.SharedResourceDefinitions.SharedResourceDefinitions.EMS";

            details.prototype.init = function(master, parent, id, name, description, handler) {
                this.jsxsuper(master, parent, id, name, description, handler);
                this.editableComponentList.addAll(["txtUserName_25F", "txtPassword_25F", "selSubtype_25F",
                        "txtURL_25F", "btnJndiConfigName_25F",
                        "txtTopicCnxFactory_25F"], this.editableComponentList.size());
                this.rwComponentList.addAll(["txtUserName_25F", "txtPassword_25F", "selSubtype_25F",
                        "txtURL_25F", "btnJndiConfigName_25F",
                        "txtTopicCnxFactory_25F", "btnTestConnection_25F"], this.rwComponentList.size());
                this.roComponentList.addAll(["lblcUserName_25F", "lblcPassword_25F", "lblcSubtype_25F",
                        "lblcURL_25F", "lblcJndiConfigName_25F",
                        "lblcTopicCnxFactory_25F"], this.roComponentList.size());
            }

            details.prototype.getType = function() {
                return tibco.admin.Main.getServer().getDynamicProperty("@SRDType_EMS");
            }

            details.prototype.getHelpId = function() {
                return this.helpID;
            }

            details.prototype.getDetailsOperation = function() {
                return "getEmsResourceDetails";
            }
            details.prototype.getDetailsOperationRules = function() {
                return "tibco/admin/enterpriseAssets/shdResDefs/EMS/rules/getEmsResourceDetails.xml";
            }
            details.prototype.editOperation = function() {
                return "editEmsResource";
            }
            details.prototype.editOperationRules = function() {
                return "tibco/admin/enterpriseAssets/shdResDefs/EMS/rules/editEmsResource.xml";
            }
            details.prototype.addOperation = function() {
                return "addEmsResource";
            }
            details.prototype.addOperationRules = function() {
                return "tibco/admin/enterpriseAssets/shdResDefs/EMS/rules/addEmsResource.xml";
            }
            details.prototype.getDetailsComponent = function() {
                return "tibco/admin/enterpriseAssets/shdResDefs/EMS/components/shdEmsResDefDetails.xml";
            }
            details.prototype.getSecondaryDetailsComponent = function() {
                return null;
            }

            // TODO: make name unique
            details.prototype.getDefaultName = function() {
                return tibco.admin.Main.getServer().getDynamicProperty("@defaultName_EMS");
            }
            details.prototype.getDefaultDescription = function() {
                return tibco.admin.Main.getServer().getDynamicProperty("@defaultDescription_EMS");
            }

            details.prototype.initializeGenericDetails = function(objEvent) {
                this.jsxsuper(objEvent);
                if(objEvent.status == "SUCCESS") {
                    this.addTestConnectionButton(false);
                }
            }

            details.prototype.initializeSpecificDetails = function(objEvent) {
                if(objEvent.status == "SUCCESS") {
                    var str = '<data jsxid="jsxroot">';
                    str += '<record jsxid="DIRECT" jsxtext="' + tibco.admin.Main.getServer().getDynamicProperty("@SubtypeDIRECT_25F") + '" />';
                    str += '<record jsxid="JNDI" jsxtext="' + tibco.admin.Main.getServer().getDynamicProperty("@SubtypeJNDI_25F") + '" />';
                    str += '</data>';
                    var sel = this.mainComponent.getDescendantOfName("selSubtype_25F");
                    tibco.admin.Main.getServer().getCache().clearById(sel.getXMLId());
                    sel.setXMLString(str);
                    sel.repaint();
                    this.mainComponent.getDescendantOfName("selSubtype_25F").subscribe(details.ONTYPESELECTED,
                            this, this.showSubDetails);
                    this.mainComponent.getDescendantOfName("btnJndiConfigName_25F").subscribe(details.ONPICKJNDI,
                            this, this.pickJNDI);
                }
                this.jsxsuper(objEvent);

                objEvent.obj.handler = this;
                //070818
            }

            details.prototype.initializeGenericAdd = function(objEvent) {
                this.jsxsuper(objEvent);
                if(objEvent.status == "SUCCESS") {
                    this.addTestConnectionButton(true);
                }
            }

            details.prototype.initializeSpecificAdd = function(objEvent) {
                if(objEvent.status == "SUCCESS") {
                    var str = '<data jsxid="jsxroot">';
                    str += '<record jsxid="DIRECT" jsxtext="' + tibco.admin.Main.getServer().getDynamicProperty("@SubtypeDIRECT_25F") + '" />';
                    str += '<record jsxid="JNDI" jsxtext="' + tibco.admin.Main.getServer().getDynamicProperty("@SubtypeJNDI_25F") + '" />';
                    str += '</data>';
                    var sel = this.mainComponent.getDescendantOfName("selSubtype_25F");
                    tibco.admin.Main.getServer().getCache().clearById(sel.getXMLId());
                    sel.setXMLString(str);
                    sel.setValue("DIRECT");
                    sel.repaint();
                    this.mainComponent.getDescendantOfName("selSubtype_25F").subscribe(details.ONTYPESELECTED,
                            this, this.showSubDetails);
                    this.mainComponent.getDescendantOfName("btnJndiConfigName_25F").subscribe(details.ONPICKJNDI,
                            this, this.pickJNDI);
                    this.mainComponent.getDescendantOfName("blkURL_25F").setDisplay(jsx3.gui.Block.DISPLAYBLOCK, true);

                    objEvent.obj.handler = this;//070818

                }
                this.jsxsuper(objEvent);
                this.initDIRECTParam(objEvent);//071006
            }

            details.prototype.addTestConnectionButton = function(enabled) {
                var button = new com.gix.gui.Button("btnTestConnection_25F", "", "", 
					tibco.admin.Main.getServer().getDynamicProperty("@addin@Btn Width"), tibco.admin.Main.getServer().getDynamicProperty("@btnTestConnection_25F"));
                button.setHeight(tibco.admin.Main.getServer().getDynamicProperty("@addin@Btn Height"));
                button.setFontName(tibco.admin.Main.getServer().getDynamicProperty("@addin@Btn Font Name"));
                button.setFontSize(tibco.admin.Main.getServer().getDynamicProperty("@addin@Btn Font Size"));
                button.setFontWeight(tibco.admin.Main.getServer().getDynamicProperty("@addin@Btn Font Weight"));
                button.setColor(tibco.admin.Main.getServer().getDynamicProperty("@addin@Btn Color"));
                button.setDisabledColor(tibco.admin.Main.getServer().getDynamicProperty("@addin@Btn Disabled Color"));
                button.setBackgroundColor(tibco.admin.Main.getServer().getDynamicProperty("@addin@Btn BGColor"));
                button.setPadding(tibco.admin.Main.getServer().getDynamicProperty("@addin@Btn Padding"));
                button.setMargin(tibco.admin.Main.getServer().getDynamicProperty("@addin@Btn Margin"));
                button.setBorder(tibco.admin.Main.getServer().getDynamicProperty("@addin@Btn Border"));
                button.setDisabledBackgroundColor(tibco.admin.Main.getServer().getDynamicProperty("@addin@Btn Disabled BGColor"));
                button.setClassName(tibco.admin.Main.getServer().getDynamicProperty("@addin@Btn Css Rule Name"));
                button.setDisplay(jsx3.gui.Block.DISPLAYNONE);
                button.subscribe(details.ONTESTCONNECTION, this, this.testConnection);
                button.setEvent('this.publish({subject:"' + details.ONTESTCONNECTION + '"});',
                        jsx3.gui.Interactive.EXECUTE);
                var pane = this.mainComponent.getDescendantOfName("paneEditActions_25");
                pane.setChild(button, jsx3.app.Model.PERSISTNONE);
                pane.adoptChild(pane.getChild("paneHelp_25"), true, false);
            }

            details.prototype.testConnection = function(event) {
                var objService = tibco.admin.Main.loadService("tibco.admin",
                        "tibco/admin/enterpriseAssets/shdResDefs/EMS/rules/testEmsResource.xml",
                        "testEmsResource", "Test EMS Shared Resource");
                objService.setEndpointURL(tibco.admin.util.ServiceHelper.getSharedResourcesEndpointURL());

                objService.name = this.mainComponent.getDescendantOfName("txtName_25").getValue();
                objService.description = this.mainComponent.getDescendantOfName("txtDescription_25").getValue();
                this.setRequestDetails(objService);

                //subscribe and call
                objService.subscribe(jsx3.net.Service.ON_SUCCESS, this, this.onCallTestConnectionSuccess);
                objService.doCall();
            }

            details.prototype.ongetConfigDetailsSuccess = function(objEvent) {
                var response = tibco.admin.Main.getServer().Cache.getDocument("xmlCacheIDGetEmsDetailsResponse_25F").getRecordNode("jsxroot");
                this.jsxsuper(response);

                value = response.getAttribute("username");
                this.mainComponent.getDescendantOfName("lblcUserName_25F").setText(value ? value : "", true);
                this.mainComponent.getDescendantOfName("txtUserName_25F").setValue(value, true);
                value = response.getAttribute("password");
                // TODO: set password field length based on real password length?
                this.mainComponent.getDescendantOfName("txtPassword_25F").setValue(value, true);
                value = response.getAttribute("subtype");
                this.mainComponent.getDescendantOfName("selSubtype_25F").setValue(value, true);
                // TODO: set callback on select button to switch displayed blocks

                switch(value) {
                    case "DIRECT":
                        this.mainComponent.getDescendantOfName("lblcSubtype_25F").setText(tibco.admin.Main.getServer().getDynamicProperty("@SubtypeDIRECT_25F"), true);
                        value = response.getAttribute("providerURL");
                        this.mainComponent.getDescendantOfName("lblcURL_25F").setText(value, true);
                        this.mainComponent.getDescendantOfName("txtURL_25F").setValue(value, true);
                        this.mainComponent.getDescendantOfName("blkURL_25F").setDisplay(jsx3.gui.Block.DISPLAYBLOCK, true);
                        this.mainComponent.getDescendantOfName("blkJndiConfigName_25F").setDisplay(jsx3.gui.Block.DISPLAYNONE, true);
                        this.mainComponent.getDescendantOfName("blkTopicCnxFactory_25F").setDisplay(jsx3.gui.Block.DISPLAYNONE, true);
                        break;
                    case "JNDI":
                        this.mainComponent.getDescendantOfName("lblcSubtype_25F").setText(tibco.admin.Main.getServer().getDynamicProperty("@SubtypeJNDI_25F"), true);
                        value = response.getAttribute("JNDISharedConfigName");
                        this.mainComponent.getDescendantOfName("lblcJndiConfigName_25F").setText(value, true);
                        this.mainComponent.getDescendantOfName("btnJndiConfigName_25F").setDynamicProperty("jsxtext", null);
                        this.mainComponent.getDescendantOfName("btnJndiConfigName_25F").setText(value, true);
                        this.mainComponent.getDescendantOfName("blkJndiConfigName_25F").setDisplay(jsx3.gui.Block.DISPLAYBLOCK, true);
                    // TODO: set callback on button to display picker dialog
                        value = response.getAttribute("topicConnectionFactory");
                        this.mainComponent.getDescendantOfName("lblcTopicCnxFactory_25F").setText(value, true);
                        this.mainComponent.getDescendantOfName("txtTopicCnxFactory_25F").setDynamicProperty("jsxvalue", null);
                        this.mainComponent.getDescendantOfName("txtTopicCnxFactory_25F").setValue(value, true);
                        this.mainComponent.getDescendantOfName("blkTopicCnxFactory_25F").setDisplay(jsx3.gui.Block.DISPLAYBLOCK, true);
                        this.mainComponent.getDescendantOfName("blkURL_25F").setDisplay(jsx3.gui.Block.DISPLAYNONE, true);
                        break;
                }
                var me = this;
                setTimeout(function() {
                    me.setEditable(true);
                    me.showSubDetails();
                },0);
            }

            details.prototype.showSubDetails = function(event) {
                var obj = tibco.admin.Main.getServer().getJSXByName("blkDetailsForType_25F");
                switch(this.mainComponent.getDescendantOfName("selSubtype_25F").getValue()) {
                    case "DIRECT":
                        this.mainComponent.getDescendantOfName("blkJndiConfigName_25F").setDisplay(jsx3.gui.Block.DISPLAYNONE, true);
                        this.mainComponent.getDescendantOfName("blkTopicCnxFactory_25F").setDisplay(jsx3.gui.Block.DISPLAYNONE, true);
                        this.mainComponent.getDescendantOfName("blkURL_25F").setDisplay(jsx3.gui.Block.DISPLAYBLOCK, true);
                        this.fixRow(this.mainComponent.getDescendantOfName("blkURL_25F"));
                        obj.adoptChild(obj.getChild("blkJndiConfigName_25F"), true);
                        obj.adoptChild(obj.getChild("blkTopicCnxFactory_25F"), true);
                        this.initDIRECTParam();//071006
                        break;
                    case "JNDI":
                        this.mainComponent.getDescendantOfName("blkURL_25F").setDisplay(jsx3.gui.Block.DISPLAYNONE, true);
                        this.mainComponent.getDescendantOfName("blkJndiConfigName_25F").setDisplay(jsx3.gui.Block.DISPLAYBLOCK, true);
                        this.mainComponent.getDescendantOfName("blkTopicCnxFactory_25F").setDisplay(jsx3.gui.Block.DISPLAYBLOCK, true);
                        this.fixRow(this.mainComponent.getDescendantOfName("blkJndiConfigName_25F"));
                        this.fixRow(this.mainComponent.getDescendantOfName("blkTopicCnxFactory_25F"));
                        obj.adoptChild(obj.getChild("blkURL_25F"), true);
                        this.initJNDIParam();//071006
                        break;
                }
            }

            details.prototype.fixRow = function(obj) {
                var count = obj.getChildren().length;
                for(var i = 0; i < count; i++) {
                    obj.adoptChild(obj.getChild(0), true);
                }
            }

            details.prototype.pickJNDI = function(event) {
                var pick = this.mainComponent.getDescendantOfName("btnJndiConfigName_25F").getText();
                if(pick == tibco.admin.Main.getServer().getDynamicProperty("@btnJndiConfigTitle_25F")) {
                    pick = null;
                }
                tibco.admin.enterpriseAssets.shdResDefs.ShdResDefPicker.pickChoice(event.source, "JNDI", pick);
            }

            details.prototype.setEditRequestDetails = function(objService) {
                this.setRequestDetails(objService);
            }

            details.prototype.setAddRequestDetails = function(objService) {
                this.setRequestDetails(objService);
            }

            details.prototype.setRequestDetails = function(objService) {
                objService.username = this.mainComponent.getDescendantOfName("txtUserName_25F").getValue();
                objService.password = this.mainComponent.getDescendantOfName("txtPassword_25F").getValue();
                objService.subtype = this.mainComponent.getDescendantOfName("selSubtype_25F").getValue();

                switch(objService.subtype) {
                    case "DIRECT":
                        objService.providerURL = this.mainComponent.getDescendantOfName("txtURL_25F").getValue();
                        break;
                    case "JNDI":
                        objService.JNDISharedConfigName = this.mainComponent.getDescendantOfName("btnJndiConfigName_25F").getText();
                        objService.topicConnectionFactory = this.mainComponent.getDescendantOfName("txtTopicCnxFactory_25F").getValue();
                        break;
                }
            }

            details.prototype.onCallTestConnectionSuccess = function(objEvent) {
                var response = tibco.admin.Main.getServer().Cache.getDocument("xmlCacheIDTestConnectionResponse_25F").getRecordNode("jsxroot");

                if(response.getAttribute("wasSuccessful") == 'true') {
                    tibco.admin.Main.alert(tibco.admin.Main.getServer().getDynamicProperty("@TestConSuccess_25F"), tibco.admin.Main.getServer().getDynamicProperty("@TestConSuccessMSG_25F"));
                }
                else {
                    tibco.admin.Main.alert(tibco.admin.Main.getServer().getDynamicProperty("@TestConFailed_25F"), response.getAttribute("summary"));
                }
            }

            /*070818
               * if user doesn't fishish all the required fields, the "save" button will be disabled.  part 2 for ems
               */
            details.prototype.valiEMSURL = function(obj, strValue) {
                this.passedItems["EMSURL"] = tibco.admin.util.Validation.doValidateNotEmptyNoWhSpace(obj, strValue, tibco.admin.Main.getServer().getDynamicProperty("@URL_25F"));
                this.enableSaveBtn4Add();
            }

            details.prototype.valiEMSTopic = function(obj, strValue) {
                this.passedItems["EMSTopic"] = tibco.admin.util.Validation.doValidateNotEmptyNoWhSpace(obj, strValue, tibco.admin.Main.getServer().getDynamicProperty("@TopicCnxFactory_25F"));
                this.enableSaveBtn4Add();
            }

            details.prototype.valiEMSJNDIName = function(obj) {
                this.passedItems["EMSJNDIName"] = (this.mainComponent.getDescendantOfName("btnJndiConfigName_25F").getText() == "Choose JNDI Configuration" || this.mainComponent.getDescendantOfName("btnJndiConfigName_25F").getText() == "")?false:true;
                //070818
                this.enableSaveBtn4Add();
            }

            // this function is used for validating the paramters when the type is JNDI 071006
            details.prototype.initJNDIParam = function(objEvent){
                this.passedItems = {};
                this.passedItems["namePassed"] = tibco.admin.util.Validation.trimAll(this.mainComponent.getDescendantOfName("txtName_25").getValue())?true:false;//070818
                this.passedItems["EMSJNDIName"] = (this.mainComponent.getDescendantOfName("btnJndiConfigName_25F").getText() == "Choose JNDI Configuration" || tibco.admin.util.Validation.trimAll(this.mainComponent.getDescendantOfName("btnJndiConfigName_25F").getText()) == "")?false:true;//070818
                this.passedItems["EMSTopic"] = tibco.admin.util.Validation.trimAll(this.mainComponent.getDescendantOfName("txtTopicCnxFactory_25F").getValue())?true:false;//070818
                this.enableSaveBtn4Add();

            }

            //this function is used for validating the paramters when the type is DIRECT  071006
            details.prototype.initDIRECTParam = function(objEvent){
                this.passedItems = {};
                this.passedItems["namePassed"] = tibco.admin.util.Validation.trimAll(this.mainComponent.getDescendantOfName("txtName_25").getValue())?true:false;//070818
                this.passedItems["EMSURL"] = tibco.admin.util.Validation.trimAll(this.mainComponent.getDescendantOfName("txtURL_25F").getValue())?true:false;//070818
                this.enableSaveBtn4Add();
            }
        });
